package com.jzo2o.customer.controller.consumer;

import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.dto.PageQueryDTO;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("addressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址簿相关接口")
@RequiredArgsConstructor
public class AddressBookController {
    private final IAddressBookService bookService;

    @PostMapping()
    @ApiOperation("新增地址")
    public void saveAddress(@RequestBody AddressBookUpsertReqDTO addressDto) {
        bookService.saveAddress(addressDto);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询地址")
    public PageResult<AddressBook> pageQuery(AddressBookPageQueryReqDTO query) {
        return bookService.pageQuery(query);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址详情")
    public AddressBook getAddressDetail(@PathVariable("id") Long id) {
        return bookService.getAddressDetail(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改地址")
    public void updateAddress(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO addressDto) {
        bookService.updateAddress(id, addressDto);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址")
    public void deleteAddressBatch(@RequestBody List<Long> ids) {
        bookService.deleteAddressBatch(ids);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefaultAddress(Long id, Integer flag) {
        bookService.setDefaultAddress(id, flag);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBook getDefaultAddress() {
        return bookService.getDefaultAddress();
    }
}
